{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import huggingface_hub\n",
    "from stable_diffusion_reference_only.pipelines.stable_diffusion_reference_only_pipeline import (\n",
    "    StableDiffusionReferenceOnlyPipeline,\n",
    ")\n",
    "import anime_segmentation\n",
    "from diffusers.schedulers import UniPCMultistepScheduler\n",
    "from PIL import Image\n",
    "import cv2\n",
    "import numpy as np\n",
    "import os\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "device = \"cuda\"\n",
    "dtype = torch.float16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "automatic_coloring_pipeline = StableDiffusionReferenceOnlyPipeline.from_pretrained(\n",
    "    \"AisingioroHao0/stable-diffusion-reference-only-automatic-coloring-0.1.2\"\n",
    ").to(device, dtype=dtype)\n",
    "automatic_coloring_pipeline.scheduler = UniPCMultistepScheduler.from_config(\n",
    "    automatic_coloring_pipeline.scheduler.config\n",
    ")\n",
    "segment_model = anime_segmentation.get_model(\n",
    "    model_path=huggingface_hub.hf_hub_download(\"skytnt/anime-seg\", \"isnetis.ckpt\")\n",
    ").to(device, dtype=dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = (\n",
    "    Image.open(\"../validation_images/3/1_capture.png\").convert(\"RGB\").resize((512, 512))\n",
    ")\n",
    "prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "blueprint = Image.open(\"../validation_images/9/2_line.png\").convert(\"RGB\").resize((512, 512))\n",
    "blueprint = Image.eval(blueprint, lambda x: 255 - x)\n",
    "blueprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = automatic_coloring_pipeline(\n",
    "    prompt=prompt, blueprint=blueprint, num_inference_steps=20\n",
    ").images[0]\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_result=Image.new(\"RGB\",(512*3,512))\n",
    "new_result.paste(prompt,(0,0))\n",
    "new_result.paste(blueprint,(512,0))\n",
    "new_result.paste(result,(1024,0))\n",
    "new_result.save(os.path.join(\"../lab_logs\",\"12.png\"))\n",
    "new_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
